package com.ea.report.service.impl;

import com.ea.common.report.pojo.vo.*;
import com.ea.report.mapper.StatsGroupEmotionalMapMapper;
import com.ea.report.mapper.StatsGroupPersonalityMapMapper;
import com.ea.report.mapper.StatsWarningPropertiesMapper;
import com.ea.report.mapper.StatsWarningTypeMapper;
import com.ea.report.pojo.po.StatsGroupEmotionalMap;
import com.ea.report.pojo.po.StatsGroupPersonalityMap;
import com.ea.report.pojo.po.StatsWarningType;
import com.ea.report.service.StatsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

@Service
public class StatsServiceImpl implements StatsService {
    @Resource
    StatsWarningTypeMapper statsWarningTypeMapper;
    @Resource
    StatsWarningPropertiesMapper statsWarningPropertiesMapper;
    @Resource
    StatsGroupEmotionalMapMapper statsGroupEmotionalMapMapper;
    @Resource
    StatsGroupPersonalityMapMapper statsGroupPersonalityMapMapper;

    // 除两个饼图以外，StartDate和EndDate区间内 每人同一类型只记最新的一次
    @Override
    public StatsWarningCountTypeVo getStatsWarningTypeVo(String startDate, String endDate, List<Long> companyIdList, List<Long> deptIdList) {
        StatsWarningType statsWarningType;
        // if (startDate == null && endDate == null) {
        statsWarningType = statsWarningTypeMapper.selectStatsWarningType(startDate, endDate, companyIdList, deptIdList);
        // }
        // else {
        //     statsWarningType = statsWarningTypeMapper.selectStatsWarningTypeByDate(startDate, endDate, companyIdList, deptIdList);
        // }
        return parseStatWarningTypeToVo(statsWarningType);
    }

    @Override
    public List<StatsWarningPropertiesVo> getStatsWarningPropertiesVo(String startDate, String endDate, List<Long> companyIdList, List<Long> deptIdList) {
        if(companyIdList==null)
            return null;
        if (companyIdList.isEmpty())
            return null;
        List<StatsWarningPropertiesVo> voList = statsWarningPropertiesMapper.selectStatsWarningProperties(startDate, endDate, companyIdList, deptIdList);
        voList.sort(Comparator.comparing(StatsWarningPropertiesVo::getIndex));
        return voList;
    }

    /**
     * 获取情绪分布图 在 startDate 与 endDate 之间筛选人员最新的情绪类型
     */
    @Override
    public StatsGroupEmotionalMapVo getStatsGroupEmotionalMapVo(String startDate, String endDate, List<Long> companyIdList, List<Long> deptIdList) {
        StatsGroupEmotionalMap statsGroupEmotionalMap = statsGroupEmotionalMapMapper.selectStatsGroupEmotionalMapByDate(startDate, endDate, companyIdList, deptIdList);
        return parseStatsGroupEmotionalMapToVo(statsGroupEmotionalMap);
    }

    /**
     * 获取性格分布图 在 startDate 与 endDate 之间筛选人员最新的性格类型
     */
    @Override
    public StatsGroupPersonalityMapVo getStatsGroupPersonalityMapVo(String startDate, String endDate, List<Long> companyIdList, List<Long> deptIdList) {
        StatsGroupPersonalityMap statsGroupPersonalityMap = statsGroupPersonalityMapMapper.selectStatsGroupPersonalityMap(startDate, endDate, companyIdList, deptIdList);
        return parseStatsGroupPersonalityMapToVo(statsGroupPersonalityMap);
    }

    @Override
    public List<StatsWarningDeptVo> getStatsWarningDeptVo(String startDate, String endDate, List<Long> companyIdList, List<Long> deptIdList) {
        return statsWarningTypeMapper.selectStatsWarningDept(startDate, endDate, companyIdList, deptIdList);
    }

    private StatsGroupPersonalityMapVo parseStatsGroupPersonalityMapToVo(StatsGroupPersonalityMap statsGroupPersonalityMap) {
        StatsGroupPersonalityMapVo vo = new StatsGroupPersonalityMapVo();
        vo.setCalmType(statsGroupPersonalityMap.getCalmType());
        vo.setEnthusiasticType(statsGroupPersonalityMap.getEnthusiasticType());
        vo.setSensitiveType(statsGroupPersonalityMap.getSensitiveType());
        vo.setExcitedType(statsGroupPersonalityMap.getExcitedType());
        return vo;
    }

    private StatsGroupEmotionalMapVo parseStatsGroupEmotionalMapToVo(StatsGroupEmotionalMap statsGroupEmotionalMap) {
        StatsGroupEmotionalMapVo vo = new StatsGroupEmotionalMapVo();
        vo.setPhysiological(statsGroupEmotionalMap.getPhysiological());
        vo.setNegative(statsGroupEmotionalMap.getNegative());
        vo.setPositive(statsGroupEmotionalMap.getPositive());
        return vo;
    }

    private StatsWarningCountTypeVo parseStatWarningTypeToVo(StatsWarningType statsWarningType) {
        StatsWarningCountTypeVo vo = new StatsWarningCountTypeVo();
        vo.setMaleType1(statsWarningType.getMaleType1());
        vo.setFemaleType1(statsWarningType.getFemaleType1());
        vo.setMaleType2(statsWarningType.getMaleType2());
        vo.setFemaleType2(statsWarningType.getFemaleType2());
        vo.setMaleType3(statsWarningType.getMaleType3());
        vo.setFemaleType3(statsWarningType.getFemaleType3());
        return vo;
    }
}
